Subclassing numpy.ndarray is a high-level architectural decision used to create domain-specific data structures that encapsulate metadata (like units, coordinates, or sampling rates) alongside raw numerical data. Unlike standard Python classes, NumPy objects are often created without calling __init__.
The Initialization Triad
Architects must account for three distinct instantiation paths where the standard constructor is bypassed:
- Explicit Construction: Using the class name (handled by
__new__). - View Casting: Reinterpreting an existing array as your subclass.
- New-from-template: Creating a slice or copy of an existing subclass instance.
The specialized __array_finalize__ hook is the convergence point where metadata is synchronized across these paths.
Behavioral Fragility
Subclassing creates a tight coupling with the NumPy C-API. Operations that return scalars (e.g., np.mean()) often "strip" the subclass identity, reverting to a standard ndarray. Metadata management is therefore a constant risk unless meticulously handled via state transitions.
isinstance(obj, np.ndarray). Otherwise, Composition (wrapping an array) is safer.